28

I have an alter section for a particular content type. In the form alter how can I find out whether it is create new form or edit form?

With dsm($form) in from alter I can get results with several differences between them. What is the best way to distinguish those from each other?

is this a good way?

    if(isset($form['nid']['#value']))
     'means in edit form'
    else 
     'means in create new from'

4 Answers 4

70

If you look at the code of node_object_prepare(), which is called from node_form() (the form builder for the node edit/create form), you will see it contains the following code.

  // If this is a new node, fill in the default values.
  if (!isset($node->nid) || isset($node->is_new)) {
    foreach (array('status', 'promote', 'sticky') as $key) {
      // Multistep node forms might have filled in something already.
      if (!isset($node->$key)) {
        $node->$key = (int) in_array($key, $node_options);
      }
    }
    global $user;
    $node->uid = $user->uid;
    $node->created = REQUEST_TIME;
  }

In an implementation of hook_form_BASE_FORM_ID_alter(), it is enough to use code similar to the following one.

function mymodule_form_node_form_alter(&$form, &$form_state) {
  $node = $form_state['node'];

  if (!isset($node->nid) || isset($node->is_new)) {
    // This is a new node.
  }
  else {
    // This is not a new node.
  }
}

If the node is new, then the form is creating a node; if the node is not new, then the form is editing an existing node.

In Drupal 8, every class implementing EntityInterface (including the Node class) override the EntityInterface::isNew() method. Checking if a node is new becomes as easy as calling $node->isNew(). Since in Drupal 8 there isn't $form_state['node'] anymore, the code becomes the following.

function mymodule_form_node_form_alter(&$form, &$form_state) {
  $node = $form_state->getFormObject()->getEntity();

  if ($node->isNew()) {
    // This is a new node.
  }
  else {
    // This is not a new node.
  }
}
4
  • Why are both conditions needed? Commented Jul 28, 2015 at 0:44
  • Because there could be a module that set the node ID for the new node, I guess.
    – avpaderno
    Commented Jul 28, 2015 at 2:27
  • better to use !empty($node->is_new) as isset($node->is_new) returns TRUE if it's set to FALSE.
    – bloke_zero
    Commented Apr 5, 2016 at 10:33
  • @bloke_zero I take Drupal is not expecting it to be set to FALSE. Indeed, if there are some bad modules that set it to FALSE instead of unsetting it, it is better to accordly change the code.
    – avpaderno
    Commented Apr 5, 2016 at 13:05
12

Yes, you have to check if the node ID exists or not.

4
/**
 * Implementation of hook_form_alter().
 */
function MY_MODULE_form_alter(&$form, $form_state, $form_id) {
  if ($form['#node'] && $form['#node']->type .'_node_form' === $form_id) {
    // Is node form.

    if ($form['#node']->nid) {
      // Is node edit form.
    }
  }
}
2

In Drupal 8 you will need the form_id variable and it will be different on each case and it will include the _edit_ in it if we are editing a node

function MODULENAME_form_alter(&$form, &$form_state, $form_id) { 
    if($form_id == '"node_article_edit_form"'){
        //edit form
    }
    if($form_id == 'node_article_form') {
        //create form
    }
}
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.